<?php


namespace app\general\controller;


class SmsRecord
{
    /**
     * 每2个小时，获取发送短信记录
     */
    public function getSmsRecord()
    {
        $id = $this->recordTimers(12);

        // 查询发送短信状态，该接口相同的数据只能获取一次
        $url = 'http://gateway.iems.net.cn/GeneralSMS/GSmsHttpReport?username=' . config('app_config.sms2')['id'] . ':' . config('app_config.sms2')['account'] . '';
        $data = array(
            "password" => config('app_config.sms2')['password'],
            "itemNum" => 100,
            "msgType" => 0,
            "getType" => 0,
        );
        $url = $this->returnUrlBindParam($url, $data);
        $result = $this->curl_get($url);
        if ($result) {
            if (count($result['item']) == count($result['item'], 1)) {
                // 一维数组
                $array = $result;
            } else {
                // 二维数组
                $array = $result['item'];
            }
            foreach ($array as $value) {
                $info = model('sq_sms_record')->where('sms_id', $value['messageId'])->find();
				$content = json_encode($value,JSON_UNESCAPED_UNICODE);
                model('sq_sms_record')->where('id', $info['id'])->update(['content' => $content]);
                if ($info && strpos($value['report'], '发送成功')) {
                    model('sq_sms_record')->where('id', $info['id'])->update(['status' => 1]);
                }
            }
        }
        $this->updateTimerType($id, 'success');
    }

    /**
     * 拼接成url?xxx=xxx
     * @return string
     */
    public function returnUrlBindParam($url, $data)
    {
        foreach ($data as $k => $value) {
            $url .= "&" . $k . "=" . $value;
        }
        return $url;
    }

    /**
     * @param $url
     * @return mixed
     * 发送get请求
     */
    function curl_get($url)
    {
        $header = array(
            'Accept: application/json',
        );
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 超时设置,以秒为单位
        curl_setopt($curl, CURLOPT_TIMEOUT, 1);

        // 超时设置，以毫秒为单位
        // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);

        // 设置请求头
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        //执行命令
        $data = curl_exec($curl);

        // 针对第三方接口返回数据处理
        if (strlen($data) == 2) {
            return [];
        }
        // 显示错误信息
        return json_decode($this->xml_to_json($data), true);
    }
	
	
	/**
     * @param $source
     * @return string
     * xml转化为json
     */
    function xml_to_json($source)
    {
        if (is_file($source)) {
            $xml_array = simplexml_load_file($source);
        } else {
            $xml_array = simplexml_load_string($source);
        }
        $json = json_encode($xml_array);
        return $json;
    }
	
	
	
    //记录定时器调用时间
    public function recordTimers($type)
    {
        $data = [
            'type' => $type,
            'create_time' => time(),
            'status' => 0
        ];
        return db('sq_record_timer')->insertGetId($data);
    }

    public function updateTimerType($id,$remark='')
    {
        db('sq_record_timer')->where('id', $id)->update(['status'=>1, 'remark'=>$remark]);
    }
}